﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using QuanLyDaiLy.Model;
using QuanLyDaiLy.Context;
using QuanLyDaiLy.Platform;


namespace QuanLyDaiLy.View
{
    /// <summary>
    /// Interaction logic for WDaiLy.xaml
    /// </summary>
    public partial class WDaiLy : Page
    {


        #region VARIABLE

        private CDaiLy daily;
        private CDaiLyContext dailyContext;

        #endregion

        #region Function

        public WDaiLy()
        {
            InitializeComponent();
            InitializeFeature();
        }

        private void InitializeFeature()
        {
            try
            {
                dailyContext = new CDaiLyContext(CConstant.CONNECTION);

                TaoBangDuLieu();
                LoadItemLoaiDaiLy();
                LoadItemQuan();
                TrangThaiMacDinh();
                CapNhatDuLieuBang();
            }
            catch { }
        }

        private void LoadItemLoaiDaiLy()
        {
            var loaidailyContext = new CLoaiDaiLyContext(CConstant.CONNECTION);
            cbLoaiDaiLy.ItemsSource = loaidailyContext.LayDanhSachTatCa();
            cbLoaiDaiLy.DisplayMemberPath = "TenLoaiDaiLy";
            cbLoaiDaiLy.SelectedItem = "MaLoaiDaily";
        }

        private void LoadItemQuan()
        {
            using (var quanContext = new CQuanContext(CConstant.CONNECTION))
            {
                cbQuan.ItemsSource = quanContext.LayDanhSachTatCa();
                cbQuan.DisplayMemberPath = "TenQuan";
                cbQuan.SelectedValue = "MaQuan";
            }
        }

        private void TaoBangDuLieu()
        {
            try
            {
                GridView grdView = new GridView();
                grdView.AllowsColumnReorder = true;
                grdView.ColumnHeaderToolTip = "DAILY";

                GridViewColumn madaily = new GridViewColumn();
                madaily.DisplayMemberBinding = new Binding("MaDaiLy");
                madaily.Header = "Mã Đại Lý";
                madaily.Width = 70;
                grdView.Columns.Add(madaily);

                GridViewColumn tendaily = new GridViewColumn();
                tendaily.DisplayMemberBinding = new Binding("TenDaiLy");
                tendaily.Header = "Tên Đại Lý";
                tendaily.Width = 90;
                grdView.Columns.Add(tendaily);

                GridViewColumn maloaidaily = new GridViewColumn();
                maloaidaily.DisplayMemberBinding = new Binding("MaLoaiDaiLy");
                maloaidaily.Header = "Loại Đại Lý";
                maloaidaily.Width = 70;
                grdView.Columns.Add(maloaidaily);

                GridViewColumn dienthoai = new GridViewColumn();
                dienthoai.DisplayMemberBinding = new Binding("DienThoai");
                dienthoai.Header = "Điện Thoại";
                dienthoai.Width = 70;
                grdView.Columns.Add(dienthoai);

                GridViewColumn diachi = new GridViewColumn();
                diachi.DisplayMemberBinding = new Binding("DiaChi");
                diachi.Header = "Địa Chỉ";
                diachi.Width = 150;
                grdView.Columns.Add(diachi);

                GridViewColumn maquan = new GridViewColumn();
                maquan.DisplayMemberBinding = new Binding("MaQuan");
                maquan.Header = "Mã Quận";
                maquan.Width = 70;
                grdView.Columns.Add(maquan);

                GridViewColumn ngaytiepnhan = new GridViewColumn();
                ngaytiepnhan.DisplayMemberBinding = new Binding("NgayTiepNhan");
                ngaytiepnhan.Header = "Ngày Tiếp Nhận";
                ngaytiepnhan.Width = 90;
                grdView.Columns.Add(ngaytiepnhan);

                GridViewColumn email = new GridViewColumn();
                email.DisplayMemberBinding = new Binding("Email");
                email.Header = "E mail";
                email.Width = 70;
                grdView.Columns.Add(email);

                GridViewColumn tienno = new GridViewColumn();
                tienno.DisplayMemberBinding = new Binding("TienNo");
                tienno.Header = "Tiền Nợ";
                tienno.Width = 70;
                grdView.Columns.Add(tienno);

                lvDaiLy.View = grdView;
            }
            catch { }
        }

        private void CapNhatDuLieuBang()
        {
            lvDaiLy.ItemsSource = dailyContext.LayDanhSachTatCa();
        }

        private void ChonDaiLy()
        {
            try
            {
                TrangThaiChon();
                if (lvDaiLy.SelectedItem is CDaiLy)
                {
                    txtTenDaiLy.Text = (lvDaiLy.SelectedItem as CDaiLy).TenDaiLy.ToString();
                    txtMaDaiLy.Text = (lvDaiLy.SelectedItem as CDaiLy).MaDaiLy.ToString();
                    using (var dailyContext = new CDaiLyContext(CConstant.CONNECTION))
                    {
                        int indext = 0;
                        foreach (CDaiLy daily in dailyContext.LayDanhSachTatCa())
                        {
                            if ((lvDaiLy.SelectedItem as CDaiLy).MaDaiLy == daily.MaDaiLy)
                            {
                                cbLoaiDaiLy.SelectedIndex = indext;
                            }
                            indext++;
                        }
                    }
                    txtDiaChi.Text = (lvDaiLy.SelectedItem as CDaiLy).DiaChi;
                    using (var quanContext = new CQuanContext(CConstant.CONNECTION))
                    {
                        int indext = 0;
                        foreach (CQuan quan in quanContext.LayDanhSachTatCa())
                        {
                            if ((lvDaiLy.SelectedItem as CDaiLy).MaQuan == quan.MaQuan)
                            {
                                cbQuan.SelectedIndex = indext;
                            }
                            indext++;
                        }
                    }
                    txtDienThoai.Text = (lvDaiLy.SelectedItem as CDaiLy).DienThoai;
                    dpNgayTiepNhan.SelectedDate = (lvDaiLy.SelectedItem as CDaiLy).NgayTiepNhan;
                    txtEmail.Text = (lvDaiLy.SelectedItem as CDaiLy).Email;
                    txtTienNo.Text = (lvDaiLy.SelectedItem as CDaiLy).TienNo.ToString();
                }
            }
            catch { }
        }

        private void XoaDaiLy()
        {
            try
            {
                var result = MessageBox.Show("Bạn Có Chắc Chắn Xóa Không?", "Thông Báo", MessageBoxButton.YesNo, MessageBoxImage.Question);
                if (result == MessageBoxResult.Yes)
                {
                    dailyContext.Xoa(Convert.ToInt32(txtMaDaiLy.Text));
                    CapNhatDuLieuBang();
                    TrangThaiMacDinh();
                    MessageBox.Show("Xóa Thành Công");
                }
            }
            catch { MessageBox.Show("Chưa Thể Xóa"); }
        }

        private void LuuDaiLy()
        {
            try
            {
                if (KiemTraNhap())
                {
                    if (Convert.ToInt32(txtMaDaiLy.Text) < dailyContext.LayMaSoMoi())
                    {
                        var result = MessageBox.Show("Bạn Có Chắc Chắn Sửa Không?", "Thông Báo", MessageBoxButton.YesNo, MessageBoxImage.Question);
                        if (result == MessageBoxResult.Yes)
                        {
                            daily = new CDaiLy(int.Parse(txtMaDaiLy.Text), (cbLoaiDaiLy.SelectedItem as CLoaiDaiLy).MaLoaiDaiLy, txtTenDaiLy.Text, txtDiaChi.Text, (cbQuan.SelectedItem as CQuan).MaQuan, txtDienThoai.Text, Convert.ToDateTime(dpNgayTiepNhan.SelectedDate.Value), txtEmail.Text, Convert.ToInt32(txtTienNo.Text));
                            dailyContext.CapNhat(daily);
                            MessageBox.Show("Sửa Thành Công");
                            ChonDaiLy();
                        }


                    }
                    else
                    {

                        daily = new CDaiLy((cbLoaiDaiLy.SelectedItem as CLoaiDaiLy).MaLoaiDaiLy, txtTenDaiLy.Text, txtDiaChi.Text, (cbQuan.SelectedItem as CQuan).MaQuan, txtDienThoai.Text, Convert.ToDateTime(dpNgayTiepNhan.SelectedDate.Value), txtEmail.Text, Convert.ToInt32(txtTienNo.Text));
                        dailyContext.Them(daily);
                        MessageBox.Show("Lưu Thành Công");
                        TrangThaiThem();
                    }

                    CapNhatDuLieuBang();
                }


            }
            catch { MessageBox.Show("Chưa Lưu Được\nVui Lòng Kiểm Tra Lại Thông Tin Nhập"); }


        }

        private void TrangThaiMacDinh()
        {
            txtTenDaiLy.IsEnabled = false;
            txtMaDaiLy.IsEnabled = false;
            cbLoaiDaiLy.IsEnabled = false;
            txtDiaChi.IsEnabled = false;
            cbQuan.IsEnabled = false;
            txtDienThoai.IsEnabled = false;
            dpNgayTiepNhan.IsEnabled = false;
            txtEmail.IsEnabled = false;
            txtTienNo.IsEnabled = false;

            txtTenDaiLy.Clear();
            txtMaDaiLy.Clear();
            cbLoaiDaiLy.Text = "Chọn Mã";
            txtDiaChi.Clear();
            cbQuan.Text = "Chọn Mã Quận";
            txtDienThoai.Clear();
            dpNgayTiepNhan.Text = DateTime.Now.ToShortDateString();
            txtEmail.Clear();

            btLuu.IsEnabled = false;
            btXoa.IsEnabled = false;
        }

        private void TrangThaiThem()
        {
            txtTenDaiLy.IsEnabled = true;
            cbLoaiDaiLy.IsEnabled = true;
            txtDiaChi.IsEnabled = true;
            cbQuan.IsEnabled = true;
            txtDienThoai.IsEnabled = true;
            dpNgayTiepNhan.IsEnabled = true;
            txtEmail.IsEnabled = true;
            txtTienNo.IsEnabled = true;

            txtTenDaiLy.Clear();
            txtMaDaiLy.Clear();
            cbLoaiDaiLy.SelectedIndex = 0;
            txtDiaChi.Clear();
            cbQuan.SelectedIndex = 0;
            txtDienThoai.Clear();
            dpNgayTiepNhan.Text = DateTime.Now.ToShortDateString();
            txtEmail.Clear();
            txtTienNo.Clear();

            btLuu.IsEnabled = true;
            btXoa.IsEnabled = false;

            txtTenDaiLy.Focus();
            txtMaDaiLy.Text = dailyContext.LayMaSoMoi().ToString();
        }

        private void TrangThaiChon()
        {
            TrangThaiThem();
            btLuu.IsEnabled = true;
            btXoa.IsEnabled = true;
        }

        private bool KiemTraNhap()
        {
            if (dpNgayTiepNhan.SelectedDate > DateTime.Now)
            {
                MessageBox.Show("Vui Lòng Kiểm Tra Lại Ngày Sinh");
                return false;
            }
            if (txtTenDaiLy.Text == "" || cbLoaiDaiLy.SelectedItem == null || txtDiaChi.Text == "" || cbQuan.SelectedItem == null || txtDienThoai.Text == "" || txtEmail.Text == "" || txtTienNo.Text == "")
            {
                MessageBox.Show("Vui Lòng Nhập Đầy Đủ Thông Tin");
                return false;
            }
            return true;
        }

        #endregion

        #region EVEN

        private void btThemDaiLy_Click(object sender, RoutedEventArgs e)
        {
            TrangThaiThem();
        }

        private void txtDienThoai_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (!Char.IsDigit((char)KeyInterop.VirtualKeyFromKey(e.Key)) &&
               !Char.IsControl((char)KeyInterop.VirtualKeyFromKey(e.Key)))
            {
                e.Handled = true;
            }
        }

        private void btLuu_Click(object sender, RoutedEventArgs e)
        {
            LuuDaiLy();
        }

        private void btXoa_Click(object sender, RoutedEventArgs e)
        {
            XoaDaiLy();
        }

        private void txtTienNo_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                LuuDaiLy();
            }
        }

        private void lvDaiLy_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ChonDaiLy();
        }

        #endregion




    }
}
